import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './plugins/element.js'

import './assets/css/global.css'
import './assets/fonts/iconfont.css'

import cookie from 'js-cookie'

import axios from 'axios'

Vue.prototype.$cookie = cookie

axios.defaults.baseURL = 'http://localhost:8080/api/v1/'
axios.defaults.headers.common.satoken = cookie.get('satoken')
axios.defaults.headers.post['Content-Type'] = 'application/json'
axios.defaults.headers.put['Content-Type'] = 'application/json'
Vue.prototype.$http = axios

axios.interceptors.request.use(
  config => {
    if (cookie.get('satoken')) {
      config.headers.satoken = cookie.get('satoken')
      // config.headers['access-control-allow-headers'] = 'Content-Type, satoken'
    }
    return config
  },
  err => {
    return Promise.reject(err)
  }
)

Vue.config.productionTip = false

router.beforeEach((to, from, next) => {
  if (to.meta.content) {
    const head = document.getElementsByTagName('head')
    const meta = document.createElement('meta')
    meta.content = to.meta.content
    head[0].appendChild(meta)
  }
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
